package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.DestinationSearch;
import cn.wolfcode.luowowo.article.mapper.DestinationSearchMapper;
import cn.wolfcode.luowowo.article.query.DestinationSearchQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationSearchService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationSearchServiceImpl implements IDestinationSearchService {

    @Autowired
    private DestinationSearchMapper destinationSearchMapper;

    @Override
    public PageInfo selectAll(DestinationSearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        String month = destinationSearchMapper.selectForMonth(qo.getMonth());
        Long[] ids = null;
        if (StringUtils.hasLength(month)){
            String[] strings = month.split(",");
            ids = new Long[strings.length];
            for (int i = 0; i < strings.length; i++) {
                ids[i] = Long.parseLong(strings[i]);
            }
        }
        qo.setDestId(ids);
        List<DestinationSearch> list = destinationSearchMapper.selectForList(qo);
        for (DestinationSearch destinationSearch : list) {
            String[] strings = destinationSearch.getAssociatedPlaces().split(",");
            List<String> places = new ArrayList<>();
            for (String string : strings) {

                places.add(string);
            }
            destinationSearch.setPlaces(places);
        }
        return new PageInfo(list);
    }
}
